package com.magina.jta.controller;

import com.magina.jta.entity.TestTableOne;
import com.magina.jta.entity.TestTableTwo;
import com.magina.jta.mapper.one.TestOneMapper;
import com.magina.jta.mapper.two.TestTwoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;

/**
 * @author Magina
 */
@RestController
@RequestMapping("/atomikos")
public class AtomikosController {

    public static final String RESULT_SUCCESS = "success";
    public static final String RESULT_FAILED = "failed";

    @Autowired
    private TestTwoMapper testTwoMapper;

    @Autowired
    private TestOneMapper testOneMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/testSuccess")
    @Transactional
    public String addIncome1(@RequestParam("name") String name, @RequestParam("amount") double amount) {
        try {
            TestTableOne testTableOne = new TestTableOne();
            testTableOne.setName(name);
            testOneMapper.insert(testTableOne);
            TestTableTwo testTableTwo = new TestTableTwo();
            testTableTwo.setUserId(testTableOne.getId());
            testTableTwo.setAmount(amount);
            testTableTwo.setOperateDate(new Timestamp(System.currentTimeMillis()));
            testTwoMapper.insert(testTableTwo);
            redisTemplate.opsForValue().set("id"+testTableOne.getId(), testTableOne.toString());
            return RESULT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return RESULT_FAILED + ":" + e.getMessage();
        }
    }

    @GetMapping("/testFail")
    @Transactional
    public String addIncome2(@RequestParam("name") String name, @RequestParam("amount") double amount) {
        try {
            TestTableOne testTableOne = new TestTableOne();
            testTableOne.setName(name);
            testOneMapper.insert(testTableOne);
            TestTableTwo testTableTwo = new TestTableTwo();
            testTableTwo.setUserId(testTableOne.getId());
            testTableTwo.setAmount(amount);
            testTableTwo.setOperateDate(new Timestamp(System.currentTimeMillis()));
            testTwoMapper.insert(testTableTwo);
            //测试发现 不能回滚redis的事务
            redisTemplate.opsForValue().set("id"+testTableOne.getId(),testTableOne.toString());
            this.throwRuntimeException();
            return RESULT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
            // return RESULT_FAILED + ":" + e.getMessage();
        }
    }

    public void throwRuntimeException() {
        throw new RuntimeException("random defined exceptions");
    }
}